* =============================================================================
* Exercises in validity - Stata code
* Advanced course in questionnaire technique and clinimetrics
*
* Reproduces Tables 1-5 and the graphs used in the exercise text.
*
* Data files (place them in the same folder as this do-file):
*   criterion validity sf12.dta   (Exercise 1.1)
*   criterion validity cancer.dta (Exercise 1.2)
*   construct validity.dta        (Exercise 1.3)
*
* Two user-written commands are needed:
*   ssc install diagt
*   search corrci        (click the link and install)
* =============================================================================

clear all
set more off

* Change to the folder containing the data files (insert your own path)
cd "C:/your/folder"


* =============================================================================
* Exercise 1.1 - criterion validity, continuous instrument and gold standard
* Variables: pcs_sf12 (PCS from SF-12), pcs_sf36 (PCS from SF-36), baseline
* =============================================================================

use "criterion validity sf12.dta", clear

* differences and averages
generate diff = pcs_sf12 - pcs_sf36
generate ave  = (pcs_sf12 + pcs_sf36)/2

* Table 1: n, mean, SD and 95% CI for SF-12, SF-36 and the difference,
*          and the paired t-test of mean difference = 0
ttest pcs_sf12 = pcs_sf36

* Limits of agreement: mean difference +/- 1.96*SD
summarize diff
display "Lower limit of agreement = " %6.2f r(mean) - invnormal(0.975)*r(sd)
display "Upper limit of agreement = " %6.2f r(mean) + invnormal(0.975)*r(sd)

* The same limits of agreement with 95% CI's
centile diff, centile(2.5 97.5) meansd

* Graph "Scatter plot.png": differences against averages
scatter diff ave, ytitle("PCS_SF12 - PCS_SF36") xtitle("(PCS_SF12 + PCS_SF36)/2") ///
    name(scat, replace)
graph export "Scatter plot.png", replace

* Bland-Altman plot: lines at 0, the mean difference and the limits of agreement
summarize diff
local m  = r(mean)
local lo = r(mean) - invnormal(0.975)*r(sd)
local hi = r(mean) + invnormal(0.975)*r(sd)
scatter diff ave, yline(0, lpattern(dash)) yline(`m') yline(`lo' `hi', lcolor(blue)) ///
    ytitle("PCS_SF12 - PCS_SF36") xtitle("(PCS_SF12 + PCS_SF36)/2") ///
    title("Bland-Altman plot") name(baplot, replace)
graph export "Bland-Altman plot.png", replace

* Graph "qq_diff_1_1.png": QQ-plot of the differences
qnorm diff, name(qq_diff, replace)
graph export "qq_diff_1_1.png", replace


* =============================================================================
* Exercise 1.2 - criterion validity, dichotomous instrument and gold standard
* Variables: test (new instrument, 1 = positive), criterion (gold standard,
*            1 = cancer)
* =============================================================================

use "criterion validity cancer.dta", clear

* Table 2: test result (rows) against gold standard (columns)
tabulate test criterion, missing

* Sensitivity, specificity, PPV and NPV "by hand" (numbers from Table 2)
display "Sensitivity = " 969/1133
display "Specificity = " 14696/14927
display "PPV         = " 969/1200
display "NPV         = " 14696/14860

* Prevalence, and the PPV and NPV expected if sensitivity = 0.85 and
* specificity = 0.95 (the researchers' hypotheses)
local p = 1133/16060
display "Prevalence   = " `p'
display "Expected PPV = " 0.85*`p' / (0.85*`p' + (1-0.95)*(1-`p'))
display "Expected NPV = " 0.95*(1-`p') / (0.95*(1-`p') + (1-0.85)*`p')

* Table 3: estimates with exact binomial 95% CI's
diagt criterion test


* =============================================================================
* Exercise 1.3 - construct validity
* Variables: whiscore7_1 (WI-7), scl8score_1 (SCL-8), pcs_1 and mcs_1 (SF-36
*            PCS and MCS), psychiatric_comorbidity (0 = no, 1 = yes),
*            gender (0 = female, 1 = male); all at baseline
* =============================================================================

use "construct validity.dta", clear

* Graph "qq_1_3.png": QQ-plots of the four scores
foreach v of varlist whiscore7_1 scl8score_1 pcs_1 mcs_1 {
    qnorm `v', name(qq_`v', replace) nodraw
}
graph combine qq_whiscore7_1 qq_scl8score_1 qq_pcs_1 qq_mcs_1, name(qq_1_3, replace)
graph export "qq_1_3.png", replace

* Graph "scatter_1_3.png": scatterplot matrix
graph matrix whiscore7_1 scl8score_1 pcs_1 mcs_1, half name(scatter_1_3, replace)
graph export "scatter_1_3.png", replace

* Table 4: Pearson correlations with 95% CI's (Fisher's z-transformation)
corrci whiscore7_1 scl8score_1
corrci whiscore7_1 mcs_1
corrci whiscore7_1 pcs_1

* Table 4: Spearman correlations with 95% CI's
* (-spearman- gives no CI, so -corrci- is used on the ranked variables)
foreach v of varlist whiscore7_1 scl8score_1 pcs_1 mcs_1 {
    egen `v'_rank = rank(`v')
}
corrci whiscore7_1_rank scl8score_1_rank
corrci whiscore7_1_rank mcs_1_rank
corrci whiscore7_1_rank pcs_1_rank

* Graphs "qq_1_3_no_comor.png" and "qq_1_3_comor.png"
qnorm whiscore7_1 if psychiatric_comorbidity==0, name(qq_no_comor, replace)
graph export "qq_1_3_no_comor.png", replace
qnorm whiscore7_1 if psychiatric_comorbidity==1, name(qq_comor, replace)
graph export "qq_1_3_comor.png", replace

* Graphs "qq_1_3_female.png" and "qq_1_3_male.png"
qnorm whiscore7_1 if gender==0, name(qq_female, replace)
graph export "qq_1_3_female.png", replace
qnorm whiscore7_1 if gender==1, name(qq_male, replace)
graph export "qq_1_3_male.png", replace

* Table 5: n, mean, SD and 95% CI by group, and the difference between groups
*          (unpaired t-test, equal standard deviations assumed)
*          The difference is group 0 minus group 1 (no - yes; women - men).
ttest whiscore7_1, by(psychiatric_comorbidity)   // H5
ttest whiscore7_1, by(gender)                    // H6

* Alternative without assuming equal standard deviations (Welch)
ttest whiscore7_1, by(psychiatric_comorbidity) unequal
ttest whiscore7_1, by(gender) unequal
